//
//  MainTabBarController.swift
//  个人微博
//
//  Created by 一介布衣 on 16/2/13.
//  Copyright © 2016年 HUAMANLOU. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 由于系统的tabBar是只读属性，不能够直接设置 可以通过kvc的方式在运行时 替换身份
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        
        // 给加号按钮添加点击事件
        mainTabBar.plusBtn.addTarget(self, action: Selector("plusBtnDidClick"), forControlEvents: .TouchUpInside)
        // 添加子控制器
        addChildViewControllers()
    }
    
    /// 加号按钮的点击事件
    @objc private func plusBtnDidClick() {
        print(__FUNCTION__)
        let composeVc = ComposeViewController()
        let nav = BaseNavigationController(rootViewController:composeVc)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    /// 添加子控制器
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    /// 添加单个的子控制器
    private func addChildViewController(vc:UIViewController, title:String, imageName:String) {
        // 设置渲染颜色
        tabBar.tintColor = UIColor.orangeColor()
        
        let nav = BaseNavigationController(rootViewController: vc)
        vc.title = title
        //        vc.tabBarItem.title = "首页"
        //        vc.navigationItem.title = "哈哈哈"
        vc.tabBarItem.image = UIImage(named: imageName)
        addChildViewController(nav)
    }
}



